﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Forms;

namespace AppearanceDetectionStation
{
    public partial class Form12 : Form
    {
        public Form12()
        {
            InitializeComponent();
            Form9.timecyc.Stop();
            Form16.timecyc.Stop();
        }
        public static System.Timers.Timer timecyc = new System.Timers.Timer();
        private void Form10_Load(object sender, EventArgs e)
        {
            timecyc.Enabled = true;
            timecyc.Interval = 3000;
            timecyc.AutoReset = true;
            timecyc.Elapsed += new System.Timers.ElapsedEventHandler(timecyc_Elapsed);
        }
        int readDone;
        void timecyc_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (readDone == 0)
            {
                List<PictureBox> PB = new List<PictureBox> { pictureBox1, pictureBox2, pictureBox3, pictureBox4, pictureBox5, pictureBox6, pictureBox7, pictureBox8, pictureBox9, pictureBox10, pictureBox11, pictureBox12, pictureBox13, pictureBox14, pictureBox15, pictureBox16, pictureBox17, pictureBox18, pictureBox19, pictureBox20, pictureBox21, pictureBox22, pictureBox23, pictureBox24, pictureBox25};
                if (FormMain.retry)
                {
                    label1.Text = "准备删除照片";
                    //deletePhtor(@"d:\相机\192.168.1.116\SD2", ".hst");
                    deletePhtor(@"d:\相机\xg\capture", ".bmp");
                    label1.Text = "删除完成";
                    readDone = 0;
                    List<PictureBox> PB1 = new List<PictureBox> { pictureBox1, pictureBox2, pictureBox3, pictureBox4, pictureBox5, pictureBox6, pictureBox7, pictureBox8, pictureBox9, pictureBox10, pictureBox11, pictureBox12, pictureBox13, pictureBox14, pictureBox15, pictureBox16, pictureBox17, pictureBox18, pictureBox19, pictureBox20, pictureBox21, pictureBox22, pictureBox23, pictureBox24, pictureBox25};
                    for (int i = 0; i < 25; i++)
                    {
                        PB1[i].Image = AppearanceDetectionStation.Properties.Resources.空调国际标志;
                    }
                    FormMain.delete = true;
                }
                string[] aryyFile = GetLatestFileNameInfo(@"d:\相机\xg\capture", ".bmp");

                if (aryyFile.Length > 0)
                {
                    //Sort(ref aryyFile);
                    DirectoryInfo d = new DirectoryInfo(@"d:\相机\xg\capture");
                    for (int i = 0; i < aryyFile.Length + 1; i++)
                    {
                        if (i > 26)
                        {
                            break;
                        }
                        try
                        {
                                label1.Text = aryyFile[i].ToString();
                                FileStream fs = new FileStream(@"d:\相机\xg\capture\" + aryyFile[i]+".bmp" +
                                    "", FileMode.Open, FileAccess.Read);
                                int byteLength = (int)fs.Length;
                                byte[] fileBytes = new byte[byteLength];
                                fs.Read(fileBytes, 0, byteLength);
                                fs.Close();
                                PB[i].Image = Image.FromStream(new MemoryStream(fileBytes));
                        }
                        catch (Exception a)
                        {
                            //label1.Text = a.ToString();
                            //throw;
                        }

                        if (FormMain.photoDone)
                        {
                            FormMain.goSelect = true;
                            //label1.Text = "查询内部码";
                            Thread.Sleep(2000);
                            if (FormMain.neibuma == "")
                            {
                                FormMain.neibuma = "无内部码" + DateTime.Now.ToString("HHmmss");
                            }
                            if (d.GetFiles().Length>0)
                            {
                                label1.Text = "保存照片";
                                CopyAllFile(@"d:\相机\xg\capture", @"d:\1\" + DateTime.Now.ToString("yyyyMMdd") + "\\" + FormMain.neibuma);
                                //label1.Text = "保存成功";
                                //readDone = 1;
                                //FormMain.goSelect = false;
                            }
                            readDone = 1;
                            label2.Text = readDone.ToString();
                            FormMain.neibuma = "";
                            FormMain.goSelect = false;
                        }
                    }
                }
            }
            else if (readDone == 1)
            {
                label1.Text = "准备删除照片";
                //deletePhtor(@"d:\相机\192.168.1.116\SD2", ".hst");
                deletePhtor(@"d:\相机\xg\capture", ".bmp");
                label1.Text = "删除完成";
                //FormMain.photoDone = false;     //删除完成后准备下一次照片存储
                readDone = 0;
                List<PictureBox> PB = new List<PictureBox> { pictureBox1, pictureBox2, pictureBox3, pictureBox4, pictureBox5, pictureBox6, pictureBox7, pictureBox8, pictureBox9, pictureBox10, pictureBox11, pictureBox12, pictureBox13, pictureBox14, pictureBox15, pictureBox16, pictureBox17, pictureBox18, pictureBox19, pictureBox20, pictureBox21, pictureBox22, pictureBox23, pictureBox24, pictureBox25 };
                for (int i = 0; i < 25; i++)
                {
                    PB[i].Image = AppearanceDetectionStation.Properties.Resources.空调国际标志;
                }
            }
        }
        //获取照片
        static string[] GetLatestFileNameInfo(string dir, string ext)
        {
            DirectoryInfo d = new DirectoryInfo(dir);
            if (d.GetFiles().Length > 0)
            {
                string[] FileName = new string[d.GetFiles().Length];
                int a = 0;
                foreach (FileInfo file in d.GetFiles())
                {
                    if (file.Extension.ToUpper() == ext.ToUpper())
                    {
                        FileName[a] = System.IO.Path.GetFileNameWithoutExtension(file.FullName);
                        a++;
                    }
                }
                return FileName;
            }
            else
            {
                string[] F = new string[0];
                return F;
            }

        }
        /// <summary>
        /// 将文件夹下的所有文件复制到另外一个文件夹
        /// </summary>
        /// <param name="sources">源目录</param>
        /// <param name="dest">目标目录</param>
        public static void CopyAllFile(string sources, string dest)
        {
            if (!Directory.Exists(dest))
            {
                Directory.CreateDirectory(dest);
            }

            string[] files = Directory.GetFileSystemEntries(sources);
            foreach (var file in files)
            {
                string destFileName = Path.Combine(dest, Path.GetFileName(file));
                if (File.Exists(file))
                {
                    File.Copy(file, destFileName, true);
                }
                else
                {
                    if (!Directory.Exists(file))
                    {
                        Directory.CreateDirectory(file);
                    }
                    else
                    {
                        CopyAllFile(Path.Combine(sources, Path.GetFileName(file)), destFileName);
                    }
                }
            }
        }

        //删除照片
        static void deletePhtor(string dir, string ext)
        {
            DirectoryInfo d = new DirectoryInfo(dir);
            //d.Delete(true);
            string FileName = "0";

            foreach (FileInfo file in d.GetFiles())
            {
                //if (file.Extension.ToUpper() == ext.ToUpper())
                //{
                    try
                    {
                        File.Delete(file.FullName);
                    }
                    catch
                    { }
                //}
            }

            //return FileName;
        }
        static void Sort(ref int[] arrNum)//传递一个数组排序
        {
            //冒泡排序
            for (int i = 0; i < arrNum.Length - 1; i++)
            {
                for (int j = 0; j < arrNum.Length - 1 - i; j++)
                {
                    if (arrNum[j] > arrNum[j + 1])
                    {
                        int temp = arrNum[j];
                        arrNum[j] = arrNum[j + 1];
                        arrNum[j + 1] = temp;
                    }
                }
            }
        }

        int x;
        int y;
        
        private void pictureBox_DoubleClick(object sender, EventArgs e)
        {
            if (sender is PictureBox pcb)
            {
                
                
                if (pcb.Size == Size)
                {
                    //pcb.Size = origSizes[pcb]; // 还原原始大小
                    //Controls.SetChildIndex(pcb, 0); // 将该pictureBox置于最底层
                    // 窗体最大化
                    //this.WindowState = FormWindowState.Maximized;

                    //// pictureBox使用Dock填充整个窗体
                    //pcb.Dock = DockStyle.Fill;
                    pcb.Size = new Size(380, 208); // 设置适合的大小
                    pcb.Location = new Point(x, y); // 设置适合的位置
                    //Controls.SetChildIndex(pcb, Controls.Count - 1); // 将该pictureBox置于最顶层
                }
                else
                {
                    //pcb.Size = Size; // 设置为整个窗体的大小
                    //Controls.SetChildIndex(pcb, Controls.Count - 1); // 将该pictureBox置于最顶层
                    // 还原窗体大小
                    //this.WindowState = FormWindowState.Normal;

                    //// pictureBox恢复原来的位置和大小
                    //pcb.Dock = DockStyle.None;
                    x = pcb.Left;
                    y = pcb.Top;
                    pcb.Size = new Size(1920, 1080); // 设置适合的大小
                    pcb.Location = new Point(0, 0); // 设置适合的位置
                    pcb.BringToFront();
                    //Controls.SetChildIndex(pcb, Controls.Count - 1); // 将该pictureBox置于最顶层
                }
            }
        }

        //private Dictionary<PictureBox, Size> origSizes = new Dictionary<PictureBox, Size>(); // 用于记录每个pictureBox的原始大小
        //private void pictureBox_DoubleClick(object sender, EventArgs e)
        //{
        //    if (sender is PictureBox pcb)
        //    {
        //        origSizes[pcb] = pcb.Size; // 记录每个pictureBox的原始大小
        //        x = pcb.Left;
        //        y = pcb.Top;              
        //        if (pcb.Size == Size)
        //        {
        //            pcb.Size = origSizes[pcb]; // 还原原始大小
        //            Controls.SetChildIndex(pcb, 0); // 将该pictureBox置于最底层
        //            // 窗体最大化
        //            //this.WindowState = FormWindowState.Maximized;

        //            //// pictureBox使用Dock填充整个窗体
        //            //pcb.Dock = DockStyle.Fill;
        //            ////pcb.Size = new Size(1920, 1060); // 设置适合的大小
        //            ////pcb.Location = new Point(0, 0); // 设置适合的位置
        //            //Controls.SetChildIndex(pcb, Controls.Count - 1); // 将该pictureBox置于最顶层
        //        }
        //        else
        //        {
        //            pcb.Size = Size; // 设置为整个窗体的大小
        //            Controls.SetChildIndex(pcb, Controls.Count - 1); // 将该pictureBox置于最顶层
        //            // 还原窗体大小
        //            //this.WindowState = FormWindowState.Normal;

        //            //// pictureBox恢复原来的位置和大小
        //            //pcb.Dock = DockStyle.None;
        //            ////pcb.Size = new Size(380, 208); // 设置适合的大小
        //            ////pcb.Location = new Point(x, y); // 设置适合的位置
        //        }
        //    }
        //}
        //private Dictionary<PictureBox, Size> origSizes = new Dictionary<PictureBox, Size>(); // 用于记录每个pictureBox的原始大小

        //private void Form1_Load(object sender, EventArgs e)
        //{
        //    // 遍历每个pictureBox控件，为其添加双击事件处理方法
        //    foreach (PictureBox pictureBox in Controls.OfType<PictureBox>())
        //    {
        //        pictureBox.DoubleClick += PictureBox_DoubleClick;
        //        origSizes[pictureBox] = pictureBox.Size; // 记录每个pictureBox的原始大小
        //    }
        //}

        //private void PictureBox_DoubleClick(object sender, EventArgs e)
        //{
        //    PictureBox pictureBox = (PictureBox)sender;

        //    if (pictureBox.Size == Size) // 如果该pictureBox已经最大化
        //    {
        //        pictureBox.Size = origSizes[pictureBox]; // 还原原始大小
        //        Controls.SetChildIndex(pictureBox, 0); // 将该pictureBox置于最底层
        //    }
        //    else
        //    {
        //        pictureBox.Size = Size; // 设置为整个窗体的大小
        //        Controls.SetChildIndex(pictureBox, Controls.Count - 1); // 将该pictureBox置于最顶层
        //    }
        //}
    }
}
